<?php
/*
 * 销售机会
*/
class Opportunity_model extends CI_Model {
	public function __construct() {
		parent :: __construct();
	}

	public function count_nums(){ /*取得总行数*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$this->db->select('*');
		$this->db->from('customer');
		$this->db->join('opportunity', 'opportunity.customer_id = customer.id');
		$this->db->join('contacter', 'contacter.id = opportunity.contacter_id');
		if($role == '市场'){
			$this->db->where('customer.user_id',$user_id);
		}
		$count = $this->db->count_all_results();
		return $count;
	}

	public function list_opportunity($limit,$offset = 0){ /*获取分页opportunity数据*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$this->db->select('customer.customer_name as customer_name,customer.user_id as customer_user_id,opportunity.id as opportunity_id,opportunity.*,contacter.id as contacter_id',false);
		$this->db->from('customer');
		$this->db->join('opportunity', 'opportunity.customer_id = customer.id');
		$this->db->join('contacter', 'contacter.id = opportunity.contacter_id');
		if($role == '市场'){
			$this->db->where('customer.user_id',$user_id);
		}
		$this->db->limit($limit,$offset);
		$this->db->order_by('opportunity.create_time', "desc");
		$query = $this->db->get();
		return $query->result();
	}
}
?>